//editables on first profile page
$.fn.editable.defaults.mode = 'inline';
$.fn.editableform.loading = "<div class='editableform-loading'><i class='light-blue icon-2x icon-spinner icon-spin'></i></div>";
$.fn.editableform.buttons = '<button type="submit" class="btn btn-info editable-submit"><i class="icon-ok icon-white"></i></button>' +
    '<button type="button" class="btn editable-cancel"><i class="icon-remove"></i></button>';

var agencyInfo;//代理机构信息列表
var agencyId = $("#agencyId").val();

initAgencyInfo();

function initAgencyInfo() {
    if (cochoApp.isNotEmpty(agencyId)) {
        getAgencyInfo();
        if (cochoApp.isNotEmpty(agencyInfo)) {
            $("#inscd").val(agencyInfo.inscd);
            $("#name").val(agencyInfo.name);
            $("#principal").val(agencyInfo.principal);
            $("#tel").val(agencyInfo.tel);
            $("#address").val(agencyInfo.address);
            $("#description").val(agencyInfo.description);
        }
    }
}

function getAgencyInfo() {
    $.ajax({
        type: "post",
        async: false,//异步，如果等于false 那么就是同步
        url: cochoApp.baseUrl + "/admin/agency/getAgencyInfoById.do?token=" + cochoApp.getTokenByStorage() + "&agencyId=" + agencyId,
        dataType: "json",
        data: "",
        success: function (data) {
            if (data != null && data.statusCode == cochoApp.SUCCESS) {
                if (data.dataRows.length > 0) {
                    agencyInfo = data.dataRows[0];
                } else {
                    cochoAlert.notificationsWarn("暂无代理机构信息！");
                }
            } else {
                cochoAlert.notificationsWarn("获取代理机构信息失败！");
            }
        },
        error: function (data) {
            cochoAlert.notificationsWarn(cochoErrorMessage.server_error_connect_admin);
        }
    });
}

var provinceList;
initProvinceSelect();
function initProvinceSelect() {
    $.ajax({
        type: "post",
        async: true,//异步，如果等于false 那么就是同步
        url: cochoApp.baseUrl + "/admin/agency/getAreaProvince.do?token=" + cochoApp.getTokenByStorage(),
        dataType: "json",
        data: "",
        success: function (data) {
            if (data != null && data.statusCode == cochoApp.SUCCESS) {
                if (data.dataRows.length > 0) {
                    provinceList = data.dataRows;
                    for (var i = 0; i < provinceList.length; i++) {
                        var areaInfo = provinceList[i];
                        if (agencyInfo != null && areaInfo.areaid == agencyInfo.province) {
                            $("#province").append("<option selected='selected' value='" + areaInfo.areaid + "'>" + areaInfo.areaname + "</option>");
                            initCitySelect();
                        } else {
                            $("#province").append("<option value='" + areaInfo.areaid + "'>" + areaInfo.areaname +  "</option>");
                            initCitySelect();
                        }
                    }
                    $("#province").chosen();
                } else {
                    cochoAlert.notificationsWarn("暂无可选信息！");
                }
            } else {
                cochoAlert.notificationsWarn("暂时无法从服务器获取信息，请重新登录或联系管理员！");
            }
        },
        error: function (data) {
            cochoAlert.notificationsWarn(cochoErrorMessage.server_error_connect_admin);
        }
    });
}


//initCitySelect();
function initCitySelect() {
    var provinceId = $("#province").val();
    //alert(provinceId);
    $("#city").empty();
    var cityList;
    //$("#city").append("<option value='请选择'>请选择</option>");
    $.ajax({
        type: "post",
        async: true,//异步，如果等于false 那么就是同步
        url: cochoApp.baseUrl + "/admin/agency/getAreaCity.do?token=" + cochoApp.getTokenByStorage() + "&provinceId=" + provinceId,
        dataType: "json",
        data: "",
        success: function (data) {
            if (data != null && data.statusCode == cochoApp.SUCCESS) {
                if (data.dataRows.length > 0) {
                    cityList = data.dataRows;
                    for (var i = 0; i < cityList.length; i++) {
                        var areaInfo = cityList[i];
                        if (agencyInfo != null && areaInfo.areaid == agencyInfo.city) {
                            $("#city").append("<option selected='selected' value='" + areaInfo.areaid + "'>" + areaInfo.areaname + "</option>");
                            initCountySelect();
                        } else {
                            $("#city").append("<option value='" + areaInfo.areaid + "'>" + areaInfo.areaname +  "</option>");
                            initCountySelect();
                        }
                    }
                    //$("#city").chosen();
                } else {
                    cochoAlert.notificationsWarn("暂无可选信息！");
                }
            } else {
                cochoAlert.notificationsWarn("暂时无法从服务器获取信息，请重新登录或联系管理员！");
            }
        },
        error: function (data) {
            cochoAlert.notificationsWarn(cochoErrorMessage.server_error_connect_admin);
        }
    });
}

//initCountySelect();
function initCountySelect() {
    var cityId = $("#city").val();
    $("#county").empty();
    //$("#county").append("<option value='请选择'>请选择</option>");
    var countyList;
    $.ajax({
        type: "post",
        async: true,//异步，如果等于false 那么就是同步
        url: cochoApp.baseUrl + "/admin/agency/getAreaCounty.do?token=" + cochoApp.getTokenByStorage()+ "&cityId=" + cityId,
        dataType: "json",
        data: "",
        success: function (data) {
            if (data != null && data.statusCode == cochoApp.SUCCESS) {
                if (data.dataRows.length > 0) {
                    countyList = data.dataRows;
                    for (var i = 0; i < countyList.length; i++) {
                        var areaInfo = countyList[i];
                        if (agencyInfo != null && areaInfo.areaid == agencyInfo.county) {
                            $("#county").append("<option selected='selected' value='" + areaInfo.areaid + "'>" + areaInfo.areaname + "</option>");
                        } else {
                            $("#county").append("<option value='" + areaInfo.areaid + "'>" + areaInfo.areaname +  "</option>");
                        }
                    }
                    //$("#county").chosen();
                } else {
                    cochoAlert.notificationsWarn("暂无可选信息！");
                }
            } else {
                cochoAlert.notificationsWarn("暂时无法从服务器获取信息，请重新登录或联系管理员！");
            }
        },
        error: function (data) {
            cochoAlert.notificationsWarn(cochoErrorMessage.server_error_connect_admin);
        }
    });
}

var levelList;//机构级别列表
initmemberLevelSelect();
function initmemberLevelSelect() {
    $.ajax({
        type: "post",
        async: true,//异步，如果等于false 那么就是同步
        url: cochoApp.baseUrl + "/members/common/getLevelInfo.do?token=" + cochoApp.getTokenByStorage(),
        dataType: "json",
        data: "",
        success: function (data) {
            if (data != null && data.statusCode == cochoApp.SUCCESS) {
                if (data.dataRows.length > 0) {
                    levelList = data.dataRows;
                    for (var i = 0; i < levelList.length; i++) {
                        var codelistInfo = levelList[i];
                        if (agencyInfo != null && codelistInfo.codevalue == agencyInfo.level) {
                            $("#level").append("<option selected='selected' value='" + codelistInfo.codevalue + "'>" + codelistInfo.codename + "</option>");
                        } else {
                            $("#level").append("<option value='" + codelistInfo.codevalue + "'>" + codelistInfo.codename +  "</option>");
                        }
                    }
                    $("#level").chosen();
                } else {
                    cochoAlert.notificationsWarn("暂无可选信息！");
                }
            } else {
                cochoAlert.notificationsWarn("暂时无法从服务器获取信息，请重新登录或联系管理员！");
            }
        },
        error: function (data) {
            cochoAlert.notificationsWarn(cochoErrorMessage.server_error_connect_admin);
        }
    });
}

function saveAnencyInfo() {
    var result = checkNullAndWarnHasNull();
    if (result == true) {//如果必填项都不为空
        var agencyId = $("#agencyId").val();
        var inscd = $("#inscd").val();
        var name = $("#name").val();
        var principal = $("#principal").val();
        var tel = $("#tel").val();
        var province = $("#province").val();
        var city = $("#city").val();
        var county = $("#county").val();
        var level = $("#level").val();
        var address = $("#address").val();
        var description = $("#description").val();
        var paramData = {"agencyId":agencyId,"inscd":inscd,"name": name,"principal": principal,"tel":tel,"province":province,"city":city,"county":county,"level":level,"address":address,"description":description, "token": cochoApp.getTokenByStorage()};
        $.ajax({
            type: "post",
            async: true,//异步，如果等于false 那么就是同步
            url: cochoApp.baseUrl + "/admin/agency/saveAgencyInfo.do",
            dataType: "json",
            data: paramData,
            success: function (data) {
                if (data != null) {
                    cochoAlert.notificationsWarn(data.statusMsg);
                    $("#agencyId").val(data.content);
                } else {
                    cochoAlert.notificationsWarn("保存代理机构信息失败！");
                }
            },
            error: function (data) {
                cochoAlert.notificationsWarn(cochoErrorMessage.server_error_connect_admin);
            }
        });
    }
}
